using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TTSoft.MyWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TTSoft.MyWeb.Models.ViewModels;
	using TTSoft.MyWeb.Models;
	using TTSoft.MyWeb.Models.Repositories;
	using TTSoft.MyWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLoaiThuoc()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteLoaiThuoc(int id, int page = 1)
		{
			var result = _repLoaiThuoc.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC,
				CurrentPage = page
			};
			return View("SelectLoaiThuoc", CreateViewModel(data));
		}

		public ActionResult EditLoaiThuoc(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditLoaiThuoc", CreateViewModel(data));
		}

		public ActionResult CancelEditingLoaiThuoc(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiThuoc", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLoaiThuoc(FormCollection forms)
		{
			string id = forms.GetValues("LoaiThuoc_ID")[0];
			string tenloaithuoc = forms.GetValues("LoaiThuoc_TenLoaiThuoc")[0];
			var loaithuoc = _repLoaiThuoc.SelectByID(int.Parse(id));
			loaithuoc.TenLoaiThuoc = tenloaithuoc;
			var result = _repLoaiThuoc.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC,
			};
			return View("SelectLoaiThuoc", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLoaiThuoc(FormCollection forms)
		{
			string tenloaithuoc = forms.GetValues("LoaiThuoc_TenLoaiThuoc")[0];
			var newItem = new LoaiThuoc
			{
				TenLoaiThuoc = tenloaithuoc
			};
			var result = _repLoaiThuoc.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAITHUOC,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectLoaiThuoc", CreateViewModel(data));
		}


	}
}